﻿using System;
using System.Collections.Generic;
using System.IO;
using SimpleFS.Core;

namespace SimpleFS.Providers.Windows
{
    public class File : IFile
    {
        internal string directory;

        internal string name;

        internal string qualifiedName;

        internal Stream stream;

        public File(string name)
        {
            System.Resources.ResourceManager srm
                = new System.Resources.ResourceManager(
                "SimpleFS", System.Reflection.Assembly.GetExecutingAssembly());

            if (name == null || name.Length == 0)
            {
                throw new ArgumentNullException("target", srm.GetString(
                    "NullTarget"));
            }
            int index = name.LastIndexOf("\\");
            if (index > 0)
            {
                this.directory = name.Substring(0, index - 1);
            }
            else
            {
                index = name.LastIndexOf("/");
                if (index > 0)
                {
                    this.directory = name.Substring(0, index - 1);
                }
            }
            this.name = name.Substring(index + 1);
            this.qualifiedName = name;
        }

        public void Copy(IFileSystemObject target)
        {
            System.Resources.ResourceManager srm 
                = new System.Resources.ResourceManager(
                "SimpleFS", System.Reflection.Assembly.GetExecutingAssembly());

            if (target == null)
            {
                throw new ArgumentNullException("target", srm.GetString(
                    "NullTarget"));
            }

            System.IO.File.Copy(this.qualifiedName, target.Name);
        }

        public void Move(IFileSystemObject target)
        {
            System.Resources.ResourceManager srm
                = new System.Resources.ResourceManager(
                "SimpleFS", System.Reflection.Assembly.GetExecutingAssembly());

            if (target == null)
            {
                throw new ArgumentNullException("target", srm.GetString(
                    "NullTarget"));
            }

            System.IO.File.Move(this.qualifiedName, target.Name);
        }

        public void Delete()
        {
            System.IO.File.Delete(this.qualifiedName);
        }

        public bool Exists()
        {
            return System.IO.File.Exists(this.qualifiedName);
        }

        public void Rename(string newName)
        {
            System.Resources.ResourceManager srm
                = new System.Resources.ResourceManager(
                "SimpleFS", System.Reflection.Assembly.GetExecutingAssembly());

            if(newName == null || newName.Length == 0)
            {
                throw new ArgumentNullException("target", srm.GetString(
                    "NullTarget"));
            }

            System.IO.File.Move(this.qualifiedName, newName);
            this.qualifiedName = newName;
        }

        public void Close()
        {
            if (this.stream != null)
            {
                this.stream.Close();
            }
        }

        #region IFile Members

        public Stream Read()
        {
            if (this.stream == null)
            {
                this.stream = System.IO.File.Open(this.qualifiedName, FileMode.Open);
            }

            return this.stream;
        }

        public void Write(Stream stream)
        {
            if (System.IO.File.Exists(this.qualifiedName))
            {
                System.IO.File.Delete(this.qualifiedName);
            }
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
            System.IO.File.WriteAllBytes(this.qualifiedName, buffer);
            stream.Seek(0, SeekOrigin.Begin);
        }

        #endregion

        #region IFileSystemObject Members

        public string Name
        {
            get { return this.qualifiedName; }
        }

        public DateTime LastModifiedTime
        {
            get { return System.IO.File.GetLastWriteTime(this.name); }
        }

        public Dictionary<string, string> Attributes
        {
            get { throw new NotImplementedException(); }
        }

        public IDirectory ParentDirectory
        {
            get 
            { 
                Directory directory = new Directory(this.directory);

                return directory;
            }
        }

        #endregion
    }
}
